<?php

	require_once(APPPATH . 'controllers/base_controllers/base_validation_controller.php');
	
	class Login_validation extends Base_validation_controller
	{
		
		public function __construct()
		{
			parent::__construct();
			
			$this->load->library('validation');
		}
		
		
		public function validate_login()
		{
			$rules['userName'] = "trim|required";
			$rules['password'] = "trim|required";
			
			$this->validation->set_rules($rules);
			
			$validationPassed = $this->validation->run();
			
			if($validationPassed)
			{
				echo( 'VALIDATION_SUCCESS' );
			}
			else
			{
				echo( $this->clean_validation_errors( $this->validation->error_string ) );
			}
			
			die;
		}
		
		
        public function validate_forgot_password()
        {
            $this->load->model('proxies/Person_proxy');

            $rules['personLN'] = "trim|required|min_length[1]|max_length[32]";
            $rules['personEmail'] = "required|valid_email";

            $personLN = $this->input->post('personLN');
            $personEmail = $this->input->post('personEmail');

            $personID = $this->Person_proxy->findPerson( $personLN, $personEmail );
            if ($personID < 1)
            {
                echo( 'COULD NOT FIND PERSON' );
                die;
            }
            
            $this->validation->set_rules($rules);
            
            $validationPassed = $this->validation->run();
            
            if($validationPassed)
            {
                echo( 'VALIDATION_SUCCESS' );
            }
            else
            {
                echo( $this->clean_validate_forgot_password_errors( $this->validation->error_string ) );
            }

            die;
        }

        public function validate_reset_password()
        {
            $rules['newpassword'] = "required|max_length[16]";
            $rules['newpassword_confirm'] = "required|max_length[16]";

            if ($this->input->post('newpassword') != $this->input->post('newpassword_confirm'))
            {
                echo( 'PASSWORD ENTRIES DO NOT MATCH' );
                die;
            }
            
            $this->validation->set_rules($rules);
            
            $validationPassed = $this->validation->run();
            
            if($validationPassed)
            {
                echo( 'VALIDATION_SUCCESS' );
            }
            else
            {
                echo( $this->clean_validate_reset_password_errors( $this->validation->error_string ) );
            }
            
            die;
        }

		private function clean_validation_errors( $valErrors )
		{
			$valErrors = str_replace("userName", "User Name", $valErrors);
			$valErrors = str_replace("password", "Password", $valErrors);
			
			return $valErrors;
		}		

        private function clean_validate_forgot_password_errors( $valErrors )
        {
            $valErrors = str_replace("personLN", "User Name", $valErrors);
            $valErrors = str_replace("password", "Password", $valErrors);
            
            return $valErrors;
        }       

        private function clean_validate_reset_password_errors( $valErrors )
        {
            $valErrors = str_replace("newpassword", "Password", $valErrors);
            $valErrors = str_replace("newpassword_confirm", "Confirm Password", $valErrors);
            
            return $valErrors;
        }       
	}

?>